﻿// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System.Collections.Generic;
using System.Data;
using System.Threading;
using System.Threading.Tasks;

namespace SnapObjects.Data.DB2
{
    internal class DB2FunctionColumnSchemaProvider : FunctionColumnSchemaProvider
    {
        private readonly ProcedureColumnSchemaProvider _columnSchemaProvider;

        public DB2FunctionColumnSchemaProvider(DataContext dataContext, ProcedureColumnSchemaProvider columnSchemaProvider)
            : base(dataContext)
        {
            _columnSchemaProvider = columnSchemaProvider;
        }

        public override IReadOnlyList<IReadOnlyList<DbColumnInfo>> GetColumnSchema(
            string function, FunctionReturnType returnType, IEnumerable<IAdoDbParameter> parameters)
        {
            return _columnSchemaProvider.GetColumnSchema(function, parameters);
        }

        public override Task<IReadOnlyList<IReadOnlyList<DbColumnInfo>>> GetColumnSchemaAsync(
            string function, FunctionReturnType returnType, IEnumerable<IAdoDbParameter> parameters, CancellationToken cancellationToken)
        {
            return _columnSchemaProvider.GetColumnSchemaAsync(function, parameters, cancellationToken);
        }

        protected override string ColumnDataTypeName(DataRow row, IDataReader reader)
        {
            var ordinal = this.ColumnOrdinal(row, reader);

            if (reader != null && ordinal > -1 && ordinal < reader.FieldCount)
            {
                return reader.GetDataTypeName(ordinal);
            }

            return null;
        }
    }
}
